package com.vv.oss.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.vv.oss.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service("ossService")
public class OssServiceImpl implements OssService {

    @Autowired
    private OSS ossClient;

    @Override
    public Map<String, String> uploadFile(MultipartFile file, String bucketName, String objectName) {
        try (InputStream inputStream = file.getInputStream()) {
            return uploadFile(inputStream, bucketName, objectName);
        } catch (IOException e) {
            throw new RuntimeException("上传文件失败", e);
        }
    }

    @Override
    public Map<String, String> uploadFile(InputStream inputStream, String bucketName, String objectName) {
        try {
            // 生成唯一文件名
            String fileName = generateUniqueFileName(objectName);
            
            // 创建PutObjectRequest对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
            
            // 上传文件
            ossClient.putObject(putObjectRequest);
            
            // 返回结果
            Map<String, String> result = new HashMap<>();
            result.put("fileName", fileName);
            result.put("url", generateFileUrl(bucketName, fileName));
            return result;
        } catch (Exception e) {
            throw new RuntimeException("上传文件失败", e);
        }
    }

    @Override
    public boolean deleteFile(String bucketName, String objectName) {
        try {
            ossClient.deleteObject(bucketName, objectName);
            return true;
        } catch (Exception e) {
            throw new RuntimeException("删除文件失败", e);
        }
    }

    /**
     * 生成唯一文件名
     * @param originalFileName 原始文件名
     * @return 唯一文件名
     */
    private String generateUniqueFileName(String originalFileName) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileExtension = getFileExtension(originalFileName);
        return uuid + fileExtension;
    }

    /**
     * 获取文件扩展名
     * @param fileName 文件名
     * @return 文件扩展名
     */
    private String getFileExtension(String fileName) {
        if (fileName == null || fileName.lastIndexOf(".") == -1) {
            return "";
        }
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /**
     * 生成文件访问URL
     * @param bucketName 存储桶名称
     * @param objectName 对象名称
     * @return 文件访问URL
     */
    private String generateFileUrl(String bucketName, String objectName) {
        return "https://" + bucketName + "." + getEndpointWithoutProtocol() + "/" + objectName;
    }

    /**
     * 获取不带协议的Endpoint
     * @return 不带协议的Endpoint
     */
    private String getEndpointWithoutProtocol() {
        //  ossClient.getEndpoint().toString();
        String endpoint ="";
        return endpoint.replace("http://", "").replace("https://", "");
    }
}